#!/bin/bash
set -e

install(){
    echo -e "\033[31m ----------------------------------------清理旧版本---------------------------------------- \033[0m"
    sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

    echo -e "\033[31m ----------------------------------------安装依赖包---------------------------------------- \033[0m"
    sudo yum install -y yum-utils \
           device-mapper-persistent-data \
           lvm2 

    echo -e "\033[31m ----------------------------------------添加yum源---------------------------------------- \033[0m"
    sudo yum-config-manager \
         --add-repo \
         https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
    sudo sed -i 's/download.docker.com/mirrors.ustc.edu.cn\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

    echo -e "\033[31m ----------------------------------------安装docker-ce---------------------------------------- \033[0m"
    sudo yum install docker-ce -y

    echo -e "\033[31m ----------------------------------------配置镜像加速---------------------------------------- \033[0m"
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
	{
  		"registry-mirrors": ["https://yeopv49g.mirror.aliyuncs.com"]
	}
	EOF
    sudo systemctl daemon-reload

    echo -e "\033[31m ----------------------------------------启动docker-ce---------------------------------------- \033[0m"
    sudo systemctl enable docker
    sudo systemctl start docker
}

main(){
    MAIN=`uname -r | awk -F . '{print $1}'`
    MINOR=`uname -r | awk -F . '{print $2}'`
    if [ $MAIN -ge 3 ] && [ $MINOR -ge 10 ];then
        install
    else
        echo -e "\033[31m 系统内核版本低于3.10，请先升级内核版本！！！! \003[0m"
        exit 1
    fi
}

main
